<template>
  <el-form
    :model="model.formData"
    ref="formRef"
    @submit.prevent
    :rules="model.formRules"
    label-width="100px"
  >
    <el-row>
      <el-col>
        <el-form-item label="说明" prop="evaluationReport">
          <el-input v-model.trim="model.formData.evaluationReport"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <!-- <el-row>
      <el-col>
        <el-form-item
          label="项目经办人"
          prop="handlederId"
        >
          <el-select
            v-model="model.formData.handlederId"
            style="width: 100%"
            filterable
            clearable
            remote
            placeholder="请输入姓名搜索"
            :remote-method="getUserData"
            value-key="id"
            @change="changeHandleder"
          >
            <el-option
              id="default-option"
              v-for="item in userList"
              :key="item.id"
              :label="item.name"
              :value="item"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row> -->
    <el-row>
      <el-col>
        <el-form-item label="附件">
          <ecm-upload
            :files="defaultFileList"
            :docid="model.docid"
            @setFile="handleSuccess"
          ></ecm-upload>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col>
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="model.formData.remark" />
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script lang="ts" setup>
import {reactive, ref,watch,defineExpose } from "vue";
import { useRoute } from "vue-router";
// import { getUsersForScience } from "/@/api/user";
import {getEvaluationById} from "/@/api/project";
import EcmUpload from "/@/components/ecmUpload/index.vue";
const props = defineProps({
  projectInfo: {
    type: Object,
    default:()=>{}
  },
});
const formRef = ref<HTMLFormElement | null>(null) as any;
const route = useRoute();
let defaultFileList = ref([]);
let userList = ref([]); // 人员列表数据
let model = reactive({
  docid: window["ecmDocType"].project,
  formData: {
    projectObj: null,
    projectCode: "",
    remark: "",
    projectId: "",
    evaluationReport: "",
    handlederId:{} as any
  },
  formRules: {
    projectId: [
      { required: true, message: "请选择要变更的项目", trigger: "change" }
    ],
    evaluationReport: [
      { required: true, message: "请输入说明", trigger: "change" }
    ],
    handlederId: [
      { required: true, message: "请选择项目经办人", trigger: "change" }
    ]
  }
});
//获取后评价详情
const initProjectInfo = async () => {
  try {
    const queryId = route.query?.projectManageId ?? "";
    const res = await getEvaluationById(queryId);
    if (res.code === 200 && res.data != null) {
      //驳回到第一个节点编辑信息，则回填发起是填写的信息
      model.formData = res.data;
      model.formData.handlederId = {
        id:+props.projectInfo.curHandleHpjUserId,
        name:props.projectInfo.curHandleHpjUserName
      }
      userList.value = [model.formData.handlederId];
      if(model.formData.evaluationReport){
        model.formData.evaluationReport = JSON.parse(res.data.evaluationReport);
      }

      //获取流程保存的信息
      if(props.projectInfo.evaluationReport){
        model.formData.evaluationReport = props.projectInfo.evaluationReport
      }
      if(props.projectInfo.remark){
        model.formData.remark = props.projectInfo.remark
      }
      if(model.formData.evaluationAttachment){
        defaultFileList.value = JSON.parse(model.formData.evaluationAttachment);
      }
    }
  } catch (err) {
    console.log(err);
  }
};

const handleSuccess = res => {
  defaultFileList.value = res.file;
  model.formData.evaluationAttachment = JSON.stringify(res.file);
};
// const getUserData = async (query = "") => {
//   if (!query) {
//     userList.value = [];
//     return;
//   }
//   try {
//     const params = {
//       pageNum: 1,
//       pageSize: 200,
//       name: query
//     };
//     const { code, data } = await getUsersForScience(params);
//     if (code === 200) {
//       userList.value = data.list.map(item => {
//         return {
//           id: item.id,
//           name: item.name + (item.deptName ? "-" + item.deptName : "-无"),
//           phoneNum: item.phoneNum,
//           email: item.email,
//           companyName: item.deptName
//         };
//       });
//     }
//   } catch (err) {
//     console.log(err);
//   }
// };

const validate = async () => {
  const isValid = await new Promise(resolve =>
    formRef.value.validate(resolve)
  );
  return isValid;
};

watch(
  () => props.projectInfo,
  newvalue => {
    if(newvalue.curHandleHpjUserId){
      initProjectInfo()
    }
  },
  { immediate: true }
);

defineExpose({
  model,
  validate
})
</script>
<style lang="scss" scoped>
.btnClass {
  margin-bottom: 10px;
}
.form-tips{
  padding-left: 100px;
  margin-bottom: 18px;
  color: #999;
  font-size: 14px;
}
</style>
